package com.rongxun.hbwallet.ui.activities;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.rongxun.base.BaseActivity;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.IntentKey;
import com.rongxun.hbwallet.ui.adapter.VerifyBackupSelectedMnemonicWordsAdapter;
import com.rongxun.utils.ActivityUtils;
import com.rongxun.utils.EventConf;
import com.rongxun.utils.EventUtil;
import com.rongxun.utils.InfoEvent;
import com.rongxun.utils.StatusBarUtil;
import com.rongxun.utils.wallet.ETHWallet;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by dwq on 2018/3/21/021.
 * e-mail:lomapa@163.com
 */

public class MnemonicBackupActivity extends BaseActivity {
    @BindView(R.id.rv_mnemonic)
    RecyclerView tvMnemonic;
    @BindView(R.id.toolbar_title)
    TextView     mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar      mToolbar;
    @BindView(R.id.btn_next)
    TextView     mBtnNext;
    @BindView(R.id.toolbar_back)
    ImageView    mToolbarBack;
    private String    walletMnemonic;
    private long      walletId;
    private ETHWallet mEthWallet;

    VerifyBackupSelectedMnemonicWordsAdapter mAdapter;

    @Override
    protected void init() {
        EventUtil.register(this);
        StatusBarUtil.setToolBarPaddingTop(mToolbar, this);
        mToolbarTitle.setText(getString(R.string.mnemonic_backup_title));
        mToolbarBack.setVisibility(View.VISIBLE);
        Intent intent = getIntent();
        mEthWallet = intent.getParcelableExtra(IntentKey.ETH_WALLET);
        walletId = mEthWallet.getId();
        walletMnemonic = mEthWallet.getMnemonic();
        String[] split = walletMnemonic.split(" ");

        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setAlignItems(AlignItems.STRETCH);

        mAdapter = new VerifyBackupSelectedMnemonicWordsAdapter(R.layout.list_item_mnemoic_selected, Arrays.asList(split));
        tvMnemonic.setLayoutManager(layoutManager);
        tvMnemonic.setAdapter(mAdapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventUtil.unregister(this);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_mnemonic_backup;
    }


    @OnClick({R.id.btn_next,R.id.toolbar_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_next:
                ActivityUtils.startActivityWithAnimotion(this,
                        new Intent(this, VerifyMnemonicBackupActivity.class)
                                .putExtra(IntentKey.ETH_WALLET, mEthWallet));
                break;
            case R.id.toolbar_back:
                onBackPressed();
                break;
        }

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(InfoEvent event) {
        if (event.id == EventConf.VERIFY_MNEMONIC_SUCCESS) {
            finish();
        }
    }
}

